home *** CD-ROM | disk | FTP | other *** search
/ Games of Daze / Infomagic - Games of Daze (Summer 1995) (Disc 1 of 2).iso / djgpp / contrib / pdcurs22 / src / unix / _kbhit.c next >
Encoding:
C/C++ Source or Header  |  1995-01-26  |  2.9 KB  |  104 lines

  1. /*
  2. ***************************************************************************
  3. * This file comprises part of PDCurses. PDCurses is Public Domain software.
  4. * You may use this code for whatever purposes you desire. This software
  5. * is provided AS IS with NO WARRANTY whatsoever.
  6. * Should this software be used in another application, an acknowledgement
  7. * that PDCurses code is used would be appreciated, but is not mandatory.
  8. *
  9. * Any changes which you make to this software which may improve or enhance
  10. * it, should be forwarded to the current maintainer for the benefit of 
  11. * other users.
  12. *
  13. * The only restriction placed on this code is that no distribution of
  14. * modified PDCurses code be made under the PDCurses name, by anyone
  15. * other than the current maintainer.
  16. * See the file maintain.er for details of the current maintainer.
  17. ***************************************************************************
  18. */
  19. #define CURSES_LIBRARY    1
  20. #include <curses.h>
  21.  
  22. #ifdef UNIX
  23. #define NOTLIB
  24. #include <defs.h>
  25. #include <term.h>
  26. #include <sys/time.h>
  27. #include <sys/types.h>
  28. #endif
  29. #undef  PDC_kbhit
  30.  
  31. #ifdef PDCDEBUG
  32. char *rcsid_PDC_kbhit  = "$Id$";
  33. #endif
  34.  
  35.  
  36.  
  37. #ifdef UNIX
  38.  
  39. /*man-start*********************************************************************
  40.  
  41.   PDC_kbhit()    - Check if a character has been entered on keyboard.
  42.  
  43.   PDCurses Description:
  44.      This is a private PDCurses routine.
  45.  
  46.      Outputs character 'chr' to screen in tty fashion. If a colour
  47.      mode is active, the character is written with colour 'colour'.
  48.  
  49.   PDCurses Return Value:
  50.      This function returns OK on success and ERR on error.
  51.  
  52.   PDCurses Errors:
  53.      No errors are defined for this function.
  54.  
  55.   Portability:
  56.      PDCurses    int PDC_putc( chtype character, chtype attr );
  57.  
  58. **man-end**********************************************************************/
  59.  
  60. int PDC_kbhit(void)
  61. {
  62.     fd_set readfds, writefds, exceptfds;
  63.     struct timeval timeout;
  64.     static struct termio    otty, ntty;
  65.     int ret;
  66.  
  67. #ifdef PDCDEBUG
  68.     if (trace_on) PDC_debug("PDC_kbhit() - called\n");
  69. #endif
  70.  
  71.     /* Create proper environment for select() */
  72.     FD_ZERO( &readfds );
  73.     FD_ZERO( &writefds );
  74.     FD_ZERO( &exceptfds );
  75.     FD_SET( fileno(stdin), &readfds );
  76.  
  77.     /* We shall specify 0.5 sec as the waiting time */
  78.     timeout.tv_sec  = 0;    /*   0 seconds */
  79.     timeout.tv_usec = 500;    /* 500 microseconds */
  80.  
  81.     /* Put tty in raw mode */
  82.     ioctl(_CUR_TERM.fd, TCGETA, &otty);
  83.     ntty = otty;
  84.     ntty.c_lflag &= ~(ECHO|ECHOE|ECHOK|ECHONL);
  85.     ntty.c_lflag &= ~ICANON;
  86.     ntty.c_lflag |= ISIG;
  87.     ntty.c_cflag &= ~(CSIZE|PARENB);
  88.     ntty.c_cflag |= CS8;
  89.     ntty.c_iflag &= (ICRNL|ISTRIP);
  90.     ntty.c_cc[VMIN] = 1;
  91.     ntty.c_cc[VTIME] = 1;
  92.     ioctl(_CUR_TERM.fd, TCSETAW, &ntty);
  93.  
  94.     /* Do a select */
  95.     ret = select( 1, &readfds, &writefds, &exceptfds, &timeout );
  96.  
  97.     /* Reset the tty back to its original mode */
  98.     ioctl(_CUR_TERM.fd, TCSETAW, &otty);
  99.  
  100.     return( ret );
  101. }
  102. #endif
  103.